package com.xoa.humanResource.controller;

import com.xoa.dao.attend.AttendMapper;
import com.xoa.humanResource.model.Integral;
import com.xoa.humanResource.service.AttendStatisticsService;
import com.xoa.humanResource.service.IntegralService;
import com.xoa.model.attend.Attend;
import com.xoa.service.attend.AttendService;
import com.xoa.service.attend.wrapper.AttendSetWrappers;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Map;

/**
 * 代码由左春晖编写更改。
 *
 * 如有需要  调用或者添加更改  请写清楚注释。
 *
 */
@Controller
@RequestMapping("/Integral")
public class IntegralController {

    @Autowired
    IntegralService  integralService;


    @Autowired
    AttendStatisticsService attendStatisticsService;

    /**
     *
     * @param request
     * @param response
     * @return
     */

    /**
     * 导出
     * @param request
     * @param response
     * @param attends
     * @return
     */
    @ResponseBody
    @RequestMapping("/queryExportAttends")  // /Integral/queryExportAttends
    public ToJson<Attend> queryExportAttends(HttpServletRequest request, HttpServletResponse response, Attend attends) {
        return attendStatisticsService.queryExportAttend(request,response,attends);
    }


    /**
     * 查询全部考勤信息
     */
    @ResponseBody
    @RequestMapping("/selsectAttendSet")   //   /Integral/selsectAttendSet
    public BaseWrapper selsectAttendSet(HttpServletRequest request){
        ContextHolder.setConsumerType("xoa"
                + (String) request.getSession().getAttribute("loginDateSouse"));
        return  attendStatisticsService.selectAttendSet();
}
    /**
     * 考勤签到
     */
    @ResponseBody
    @RequestMapping("/addAttend")    //   /Integral
    public BaseWrapper addAttend(Attend attend, HttpServletRequest req, String attendDate, String uid)throws ParseException {
        return attendStatisticsService.newAttend(attend,req,attendDate,uid);}

    /**
     *考勤类型统计   如迟到统计  早退统计  需要前台传需要查询时间
     */
    @ResponseBody
    @RequestMapping("/myAttendance")    //   /Integral/myAttendance
    public BaseWrapper myAttendance(String date,Integer uid,String strat,String end,HttpServletRequest req) throws ParseException {

        return attendStatisticsService.myAttend(date,uid,strat,end,req);
    }

    /**
     *
     * 考勤时间获取
     * @param uid
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectAttendDate")    //   /Integral/selectAttendDate
    public  ToJson selectAttendDate(Integer uid){

        return  attendStatisticsService.selectAttendDate(uid);
    }

    /**
     * 考勤信息查询
     * 调用标准产品中的考勤统计功能
     * @param beiginDate   开始查询的时间
     * @param endDate      结束查询的时间
     * @param deptId       考勤类型种类
     * @param dutyType
     * @param uids
     * @param req
     * @return
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping("/TotalAttendanceStatistics")    //   /Integral/TotalAttendanceStatistics
    public AttendSetWrappers TotalAttendanceStatistics(String beiginDate, String endDate, String deptId, String dutyType,
                                                       String uids, HttpServletRequest req) throws ParseException {
        return attendStatisticsService.TotalAttendanceStatistics(beiginDate, endDate, deptId, dutyType, uids, req);
    }

    /**
     * 查询登陆人当天的考勤
     */
    @ResponseBody
    @RequestMapping("/PersonalTodayAttendance")   //   /Integral/PersonalTodayAttendance
    public BaseWrapper PersonalTodayAttendance(String date,HttpServletRequest request) throws ParseException {
        return attendStatisticsService.PersonalTodayAttendance(date,request);}

    /**
     * 查询登陆人一段时间的考勤
     * @param beginDate
     * @param endDate
     * @param req
     * @return
     */
    @ResponseBody
    @RequestMapping("/AttendanceStatistics")   //   /Integral/AttendanceStatistics
    public AttendSetWrappers AttendanceStatistics(String beginDate, String endDate,HttpServletRequest req){
        return  attendStatisticsService.AttendanceStatistics(beginDate,endDate,req);}


    /**
     *左春晖  18-4-16  根据ID查询个人在位时长
     */
    @ResponseBody
    @RequestMapping("/selectAttendDateExamine")       // /Integral/selectAttendDateExamine
    public ToJson  selectAttendDateExamine (Attend attend) {
        return  attendStatisticsService.selectAttendDateExamine(attend);}

    /**
     * 左春晖 18-4-20  根据时间查询全体在位时长（代码有问题还在重构中）
      */
    @ResponseBody
    @RequestMapping("/AllselectAttendDateExamines")   // /Integral/AllselectAttendDateExamines
    public ToJson  AllselectAttendDateExamines(Attend attend){
        return  attendStatisticsService.AllselectAttendDateExamine(attend);
    }
    /**
     * 左春晖
     * 说明：添加
     */
    @ResponseBody
    @RequestMapping("/addIntegral")    //   /Integral
    public ToJson<Integral> addIntegral(Integral integral){
        return integralService.add(integral);
    }


    /**
     * 左春晖
     * 说明：删除积分。
     */
    @ResponseBody
    @RequestMapping("/deleteIntegralId")    //   /Integral
    public ToJson<Integral> deleteIntegral(int id){
        return integralService.delete(id);
    }


    /**
     * 左春晖
     * 查询编辑显示
     */
    @ResponseBody
    @RequestMapping("/selectById")     //   /Integral
    public ToJson<Integral> selectById(int id){ return integralService.select(id);}
    /**
     * 左春晖
     * 说明：编辑更新积分
     */

    @ResponseBody
    @RequestMapping("/updateIntegral")    //   /Integral
    public ToJson<Integral> updateIntegral(Integral integral){
        return integralService.update(integral);
    }

    /**
     * 左春晖
     * 查询直接对象的考勤信息.分页。
     */
    @ResponseBody
    @RequestMapping("/selectIntegral")    //   /Integral
    public ToJson<Integral> selectIntegral(Integer page, Integer pageSize, boolean useFlag){ return integralService.select(page,pageSize,useFlag);
    }


    //    积分设置
    @RequestMapping("/integralSet")    //   /Integral
    public String integralSet(HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        return "app/attend/integralManagement/integralSet";
    }


}
